package com.jwcao.common.cast.annotation.factory;

import com.jwcao.common.cast.annotation.CastFromCallBack;
import com.jwcao.common.cast.annotation.CastToCallBack;
import com.jwcao.common.scan.object.ScanClass;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.apache.commons.lang3.StringUtils;

/**
 * name: CastCallBackFactory description: TODO
 *
 * @author caojingwei date: 2023/3/25 13:03
 */
public class CastCallBackFactory {

    public static Method getFrom(String id, ScanClass targetClass) {
        return Arrays.stream(targetClass.getClazz().getDeclaredMethods())
            .filter(m -> m.isAnnotationPresent(CastFromCallBack.class))
            .filter(m -> {
                CastFromCallBack castFromCallBack = m.getAnnotation(CastFromCallBack.class);
                return StringUtils.equals(castFromCallBack.id(), id);
            }).findAny().orElse(null);

    }

    public static Method getTo(String id, ScanClass sourceClass) {
        return Arrays.stream(sourceClass.getClazz().getDeclaredMethods())
            .filter(m -> m.isAnnotationPresent(CastToCallBack.class))
            .filter(m -> {
                CastToCallBack castToCallBack = m.getAnnotation(CastToCallBack.class);
                return StringUtils.equals(castToCallBack.id(), id);
            }).findAny().orElse(null);
    }
}
